package cm.word.generate.web;

import cm.word.generate.dao.pgsql.PgsqlQueryMapper;
import cm.word.generate.pojo.PgsqlWordDto;
import cm.word.generate.utils.JacobOperaword;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.File;
import java.util.List;

/**
 * @author caomin
 * @date 2018/5/7
 * @说明 word
 */
@RequestMapping("/pgsql")
public class PgsqlWordController {

    @Autowired
    PgsqlQueryMapper pgsqlQueryMapper;

    /**
     * 生成 pgsql数据库 表结构
     *
     * @throws Exception
     */
    @GetMapping("/")
    public void genPgsqlData() throws Exception {

        /**
         * 生成目录
         */
        String targetFilePath = "d:/1023test.doc";

        File file = new File(targetFilePath);
        if (file.getParentFile() == null) {
            file.mkdirs();
        }
        JacobOperaword jacobOperaword = new JacobOperaword(true);
        //创建文档
        jacobOperaword.createNewDocument();
        jacobOperaword.moveDown(10);
        List<String> zlpc = pgsqlQueryMapper.getTables();
        for (int i = 0; i < zlpc.size(); i++) {
            String tableName = zlpc.get(i);
            List<PgsqlWordDto> wordList = pgsqlQueryMapper.getColumnList(tableName);
            String tableComment = pgsqlQueryMapper.getTableComment(tableName);
            System.out.println(tableName);
            int tableIndex = i + 1;
            //创建表格的行数与列数
            int cols = 6, rows = wordList.size() + 1;
            String text = tableName + "表详细设计";
            String newText = tableComment + "(" + tableName + ")";
            jacobOperaword.insertText(newText);
            jacobOperaword.moveDown(2);
            jacobOperaword.moveRight(2);
            jacobOperaword.createTable(text, cols, rows);
            jacobOperaword.putTxtToCell(tableIndex, 1, 1, "代码");
            jacobOperaword.putTxtToCell(tableIndex, 1, 2, "名称");
            jacobOperaword.putTxtToCell(tableIndex, 1, 3, "数据类型");
            jacobOperaword.putTxtToCell(tableIndex, 1, 4, "是否为主键");
            jacobOperaword.putTxtToCell(tableIndex, 1, 5, "是否为空");
            jacobOperaword.putTxtToCell(tableIndex, 1, 6, "注释");
            for (int j = 0; j < wordList.size(); j++) {
                PgsqlWordDto pgsqlWordDto = wordList.get(j);
                jacobOperaword.putTxtToCell(tableIndex, j + 2, 1, pgsqlWordDto.getColumnName());
                jacobOperaword.putTxtToCell(tableIndex, j + 2, 2, pgsqlWordDto.getBz());
                jacobOperaword.putTxtToCell(tableIndex, j + 2, 3, pgsqlWordDto.getColumnType());

                //是否为主键
                String isPrimarkey = "FALSE";
                if (pgsqlWordDto.getColumnName().equalsIgnoreCase("id")) {
                    isPrimarkey = "TRUE";
                }
                jacobOperaword.putTxtToCell(tableIndex, j + 2, 4, isPrimarkey);

                //是否为空
                Boolean sfwk = pgsqlWordDto.isSfwk();
                String sfwkString = "FALSE";
                if (sfwk != null && sfwk == true) {
                    sfwkString = "TRUE";
                }
                jacobOperaword.putTxtToCell(tableIndex, j + 2, 5, sfwkString);
                jacobOperaword.putTxtToCell(tableIndex, j + 2, 6, pgsqlWordDto.getBz());
            }
            jacobOperaword.moveDown(3);
            jacobOperaword.insertText("我是华丽的分隔线");
        }
        jacobOperaword.save(targetFilePath);
        jacobOperaword.setSaveOnExit(true);
        jacobOperaword.closeDocumentWithoutSave();
        jacobOperaword.closeDocument();
        jacobOperaword.close();
    }
}
